<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends Admin_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->library('form_validation');
    }

    public function index($page = 1)
    {
        $data['title']    = '默认首页';
        $data['siderbar'] = 'home';
        $data['submenu']  = 'home';
        // 查询日志
        $this->db->where('t <', time()-60*60);
        $this->db->delete('logs');
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        if ($this->auth->is_super_admin()) {
            $config['total_rows'] = $this->db->count_all('logs');
        } else {
            $config['total_rows'] = $this->db->where("user_id", $this->uid)->count_all('logs');
        }
        $config['base_url']  = site_url('manage/home/index/');
        $config['per_page']  = $limit;
        $config['num_links'] = 10;
        $start               = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        if ($this->auth->is_super_admin()) {
            $logs = $this->logs_m->get_all_logs($start, $limit);
        } else {
            $logs = $this->logs_m->get_all_logs($start, $limit, $this->uid);
        }
        if ($logs) {
            foreach ($logs as $k => $v) {
                if (!empty($v['uid'])) {
                    $user = $this->user_m->findBy(['user_id' => $v['uid']], "user_id,user_name,user_uname");
                    $logs[$k]['user_name'] = !empty($user['user_name']) ? $user['user_name'] : ($user['user_uname'] ?? "");
                } else {
                    $logs[$k]['user_name'] = "--";
                }
            }
        }
        $data["logs"] = $logs;
        $this->load->view('index/home', $data);
    }
}